/*++****************************************************************************
*
*	Name: main.c
*  
*	Returns: 0 = success, non-0 = error
*
*	Revision History:
*	Date	Who	Comments	
*	------	---	---------------------------------------------------------------
*	100209	DHT	Created
****************************************************************************--*/
#include <p32xxxx.h>
#include <plib.h>
#include "lib_usb_cdc.h"

// Configuration Bit settings
// SYSCLK = 80 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 80 MHz
// Primary Osc w/PLL (XT+,HS+,EC+PLL)

#if (__PIC32_FEATURE_SET__ >= 300)
#define CPU_FREQ 80000000L				// cpu clock of PIC32 = 80Mhz
#else
#define CPU_FREQ 40000000L				// cpu clock of PIC32 = 40Mhz
#endif

#pragma config FUSBIDIO = OFF
#pragma config UPLLEN   = ON        	// USB PLL Enabled
#pragma config FPLLMUL  = MUL_20        // PLL Multiplier
#pragma config UPLLIDIV = DIV_2         // USB PLL Input Divider
#pragma config FPLLIDIV = DIV_2         // PLL Input Divider
#if (__PIC32_FEATURE_SET__ >= 300)
#pragma config FPLLODIV = DIV_1         // PLL Output Divider
#else
#pragma config FPLLODIV = DIV_2         // PLL Output Divider
#endif
#pragma config FPBDIV   = DIV_1         // Peripheral Clock divisor
#pragma config FWDTEN   = OFF           // Watchdog Timer
#pragma config WDTPS    = PS1           // Watchdog Timer Postscale
#pragma config FCKSM    = CSDCMD        // Clock Switching & Fail Safe Clock Monitor
#pragma config OSCIOFNC = OFF           // CLKO Enable
#pragma config POSCMOD  = HS            // Primary Oscillator
//#pragma config IESO     = OFF         // Internal/External Switch-over
#pragma config FSOSCEN  = OFF           // Secondary Oscillator Enable (KLO was off)
#pragma config FNOSC    = PRIPLL        // Oscillator Selection
#pragma config CP       = OFF           // Code Protect
#pragma config BWP      = OFF           // Boot Flash Write Protect
#pragma config PWP      = OFF           // Program Flash Write Protect

#if (__PIC32_FEATURE_SET__ < 300)
#pragma config JTAGEN   = OFF
#pragma config ICESEL   = ICS_PGx1      // ICE/ICD Comm Channel Select
#else
#pragma config ICESEL   = ICS_PGx2      // ICE/ICD Comm Channel Select
#endif


/*********************************************************************
 * Function:        int main()
 *
 * Overview:		Main function
 *
 * PreCondition:    None
 *
 * Input:			None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Note:            None
 ********************************************************************/
int main()
{		
	INT32 rc;									// return code	

	UINT32 pbClk = SYSTEMConfigPerformance(CPU_FREQ);			// optimize PIC32 settings
	
	UINT8 buffer[] = {'h','e','l','l','o','\n'};
	
	int i;
	
	// enable interrupts
	INTEnableSystemMultiVectoredInt();		
		
	lib_usb_cdc_open();
	
	while(1)
	{
		lib_usb_cdc_write_blocked(buffer, sizeof(buffer));

		for (i = 0; i < 2000000; i++); 
				
		lib_usb_cdc_process_main();
	}	
	
	return rc;
}
